/**
 * 
 */
package com.sduept.nwld.dataserver.timer;

import com.sduept.bigdata.fault.manager.FaultExtendManager;
import com.sduept.bigdata.fault.special.maintenance.manager.MaintenanceManager;
import com.sduept.bigdata.fault.strategy.EfileCheckResult;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.daily.EquipmentOverhaulSegmentInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.*;

/**
 * 
 * Simple to Introduction
 * 
 * @Description: 功能：定时提取出设备检修的"传动"完成时间，存储到特维厂站;删除超出检修计划结束时间一个月的EquipmentOverhaulSegmentInfo数据。
 *               查询OMS的逻辑描述：只要当前时间处于检修计划中的[批准停电开始时间，批准停电结束时间]区间,就会一直查询OMS的检修信息,处于检修状态则更新特维设备信息。
 *               具体如下： 
 *               如果当前时间早于批准停电开始时间，不查询；
 *               如果当前时间在[批准停电开始时间，批准停电结束时间 ]区间，查询数据库中status = 1的数据，判断断电情况，执行“提取传动时间”操作。
 *               如果当前时间晚于批准停电结束时间，改状态为2。
 * 
 * @Author: [lixz]
 * @CreateDate: [2019年7月30日 下午5:33:43]
 * 
 */
@Service
public class SpecialEquipmentTimeTimerSchedule {
	@Inject
	private SuperDAO dao;
	@Inject
	private MaintenanceManager maintenanceManager;
	@Inject
	private FaultExtendManager faultExtendManager;

	/**
	 * 定时执行的方法，具体参数在jboss-ejb3.xml配置
	 */
	public void doWork() {
		// 删除超过一个月的数据
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -1);
		Date lastMonth = c.getTime();
		try {
			List<EquipmentOverhaulSegmentInfo> deletelist = dao.findByCondition(" batchPowerEndtTime < ? ",
					new Object[] { lastMonth }, EquipmentOverhaulSegmentInfo.class);
			if (deletelist != null) {
				for (EquipmentOverhaulSegmentInfo equipmentOverhaulSegmentInfo : deletelist) {
					dao.delete(equipmentOverhaulSegmentInfo.getId(), EquipmentOverhaulSegmentInfo.class);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<EquipmentOverhaulSegmentInfo> list = dao.findByCondition(" status = 1", null,
				EquipmentOverhaulSegmentInfo.class);
		if (list == null || list.size() == 0) {
			return;
		}
		for (EquipmentOverhaulSegmentInfo equipmentOverhaulSegmentInfo : list) {
			Date startDate = equipmentOverhaulSegmentInfo.getBatchPowerStartTime();
			Date endDate = equipmentOverhaulSegmentInfo.getBatchPowerEndtTime();
			Date currentDate = new Date();
			if (currentDate.before(startDate)) {
				// 不读取EMS断电信息
				continue;
			} else {
				if (currentDate.before(endDate)) {
					// 查EMS的检修设备信息。
					List<String> primaryEquipmentIds = findEMSOverhaulEquipmentInfo(equipmentOverhaulSegmentInfo,
							currentDate);
					if (primaryEquipmentIds != null && primaryEquipmentIds.size() > 0) {
						maintenanceManager.updateMaintenanceStationData(currentDate, primaryEquipmentIds,
								equipmentOverhaulSegmentInfo.getWorkingPlaceId());
						equipmentOverhaulSegmentInfo.setUsingTime(currentDate);
					}
				} else {
					// 超过批准检修日期，不读取EMS断电信息，更改数据状态为2
					equipmentOverhaulSegmentInfo.setStatus("2");
					//equipmentOverhaulDelay(equipmentOverhaulSegmentInfo,currentDate);
				}
			}
		}
	}

	/**
	 * 查询处于检修情况下的设备
	 * 
	 * @param equipmentOverhaulSegmentInfo 设备检修部分信息
	 * @param currentDate                  当前时间
	 */
	private List<String> findEMSOverhaulEquipmentInfo(EquipmentOverhaulSegmentInfo equipmentOverhaulSegmentInfo,
			Date currentDate) {
		List<String> eids = new ArrayList<>();
		if (StringUtils.isNotEmpty(equipmentOverhaulSegmentInfo.getReferPrimaryEquipments())) {
			String[] primaryEquipmentIds = equipmentOverhaulSegmentInfo.getReferPrimaryEquipments().split(";");
			// 通过E文件一次获取多个设备id的检修信息
			Map<String, EfileCheckResult> mapResult = faultExtendManager.powerCutCheckByEfile(currentDate,primaryEquipmentIds);
			for (String keyStr : mapResult.keySet()) {
				if (mapResult.get(keyStr).getConclusion() == 1) {
					eids.add(keyStr);
				}
			}
		}
		return eids;
	}
	/**
	 * 当时间超过检修批准结束时间时，还有设备处于检修状态的情况
	 * @param equipmentOverhaulSegmentInfo
	 * @param currentDate
	 */
	@SuppressWarnings("unused")
	private void equipmentOverhaulDelay(EquipmentOverhaulSegmentInfo equipmentOverhaulSegmentInfo, Date currentDate ) {
		List<String> primaryEquipmentIds = findEMSOverhaulEquipmentInfo(equipmentOverhaulSegmentInfo,
				currentDate);
		if (primaryEquipmentIds == null || primaryEquipmentIds.size() == 0) {
			// 如果超过批准检修日期，之后没有设备处于检修状态，不读取EMS断电信息，更改数据状态为2
			equipmentOverhaulSegmentInfo.setStatus("2");
		}
		if (primaryEquipmentIds != null && primaryEquipmentIds.size() > 0) {
			// 如果超过批准检修日期，之后还有设备处于检修状态，则更新
			maintenanceManager.updateMaintenanceStationData(currentDate, primaryEquipmentIds,
					equipmentOverhaulSegmentInfo.getWorkingPlaceId());
			equipmentOverhaulSegmentInfo.setUsingTime(currentDate);
		}
	}

}
